package com.zhuzi.demo.controller;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import com.zhuzi.common.R;
import com.zhuzi.demo.entity.Student;
import com.zhuzi.demo.repository.StudentRepository;
import com.zhuzi.demo.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhuzi.demo.model.Item;

import javax.annotation.PostConstruct;

@Slf4j
@RestController
@RequestMapping("/api")
public class ItemController {

	@Autowired
	private ItemService itemService;

	@Autowired
	private StudentRepository studentRepository;


	@RequestMapping("/items")
	public List<Item> getItems() throws IOException {
		String filePath = new ClassPathResource("").getFile().getPath() + File.separator;
		File.createTempFile(filePath, "ab");

		return itemService.getItems();
	}

	@RequestMapping("/dir")
	public R dir() throws IOException {
		String filePath = new ClassPathResource("").getFile().getPath() + File.separator;
		File.createTempFile(filePath, "ab");
		return R.ok().info(filePath);
	}

	@PostConstruct
	public void addStudent() {
		for (int i = 0; i < 10; i++) {
			Student student = new Student().setBirthTime(LocalDateTime.now()).setName("nam" + i);
			student.setId((long) (91 + i));
			log.info("添加[{}]",student);

			studentRepository.save(student);

		}
	}
}
